package com.example.leero.graduation.Util;

import java.math.BigInteger;
import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;


public class StringUtil {

  /**
   * 判断是否为空
   * @param str
   * @return
   */
  public static boolean isEmpty(String str) {
      return (str == null || str.trim().length() == 0);
  }

  /**
   * 判断字符串是否不为空
   *
   * @param str
   * @return
   */
  public static boolean isNotEmpty(String str) {
      return !isEmpty(str);
  }

  /**
   * 判断字符串长度
   * @param str
   * @param length
   * @return
   */
  public static boolean isLengthEqual(String str, int length) {
    return isNotEmpty(str) && str.length() == length;
  }

  /**
   * 判断是不是一个合法手机号码
   *
   * @param mobile
   * @return
   */
  public static boolean isMobile(String mobile) {
    if (mobile == null || mobile.trim().length() == 0) {
        return false;
    }

    return mobile.trim().length() == 11;
  }

  public static boolean notEquals(String src, String target) {
      return !equals(src, target);
  }

  public static boolean equals(String src, String target) {
      if (isEmpty(src) || isEmpty(target))
          return false;
      return src.equals(target);
  }

  /**
   * 获取MD5
   * @param s
   * @return
   */
  public static String md5(String s)
  {
      MessageDigest digest;
      try
      {
          digest = MessageDigest.getInstance("MD5");
          digest.update(s.getBytes(Charset.forName("US-ASCII")),0,s.length());
          byte[] magnitude = digest.digest();
          BigInteger bi = new BigInteger(1, magnitude);
          String hash = String.format("%0" + (magnitude.length << 1) + "x", bi);
          return hash;
      }
      catch (NoSuchAlgorithmException e)
      {
          e.printStackTrace();
      }
      return "";
  }

}
